import java.io.*;
import java.util.*;
/**
 * This is a truck class that has a major bulk of the code that is used in this program. It has all the nmethods that are required 
 * for the program to run. It has several attributes that help calculate the total. It also has two ArrayLists that store all the 
 * package info. The first one has all the packages and the second one only loaded packages;
 * This class contains all the methods that are called from main. This also sorts the array of loaded packages like specified
 * by the instructor. It catches all of the exceptions that are needed to be caught and does all it needs to do.
 *
 * <p><b>
 * Extra Credit:
 * </b><pre>
 * No extra credit was offered on this assignment
 * </pre><b>
 *
 * History:
 * </b><pre>
 * 5/04/2007 started writing the program
 * 5/08/2007 continued working on it
 * 5/09/2007 finished writing the program
 * </pre>
 *
 * @author  Veniamin Kozubenko
 * @see     "No Borrowed Code" 
 *     	
 */

public class Truck
{
	private String _driversName;
	private int _numberOfPackages;
	private int _totalPounds = 0; // total weight of the boxes
	private int _totalOunces = 0; // total weight of the letters\
	private ArrayList <Package> _packageArray = new ArrayList <Package>();
	private ArrayList <Package> _packageArray2 = new ArrayList <Package>();
	
	public void readDriverInfoAndNumberOfPackages(Scanner fin, PrintWriter fout)
	{
		/*ans bl*/
        _driversName = fin.nextLine();
		_numberOfPackages = fin.nextInt();
		fin.nextLine();
		String temp = "THIS IS WHAT THE PROGRAM WROTE TO FILE CALLED log.txt\n\n\n";
		temp += "Driver name: " + _driversName;
		temp += "\nMaximum packages truck can carry: " + _numberOfPackages;
		fout.println(temp);
		System.out.println(temp);
        /*ans ab*/
	} // End method assignDriverInfo
	
	
	public void loadTruckWithPackages(Scanner fin, PrintWriter fout)
	{
		String temporary = "\n\nPACKAGE LOADING INFORMATION:";
		temporary += "\n----------------------------";
		System.out.println(temporary);
		fout.println(temporary);


		String stringWeight, stringLength, stringWidth, stringHeight; // String representation of weight, length, etc.
		int trackingNumber;
		int intWeight = 0; 
		int intLength = 0;
		int intWidth = 0;
		int intHeight = 0; // int representation of weight, length, etc.

		int numberOfPackages = 1;
		int x = 0;
		while(fin.hasNext())
	
		{
			trackingNumber = fin.nextInt();
			fin.nextLine();
			
			try
			{
				if ((trackingNumber % 10) == 1) // Its a box (maximum weight 100 pounds and each side is 36 inches)
					{
						stringWeight = fin.nextLine();
						stringLength = fin.nextLine();
						stringWidth = fin.nextLine();
						stringHeight = fin.nextLine();
						
						try
						{	intWeight = Integer.parseInt(stringWeight);
							intLength = Integer.parseInt(stringLength);
							intWidth = Integer.parseInt(stringWidth);
							intHeight = Integer.parseInt(stringHeight);
							_packageArray.add(new Box(trackingNumber, intWeight, intLength, intWidth, intHeight));
							System.out.println(_packageArray.get(x));
							
							fout.println(_packageArray.get(x));
							x ++;
						
							if(intWeight > 100 || intWidth > 36 || intLength > 36 || intHeight > 36 || numberOfPackages > _numberOfPackages)
								throw new PackageException("NOT LOADED: ");
								
								_packageArray2.add(_packageArray.get(x - 1));
								System.out.println("LOADED");
								fout.println("LOADED");
								_totalPounds += intWeight;
								numberOfPackages ++;
								
						} //End inner try
						catch(PackageException e)
						{
							String temp = e.getMessage();
							if (intWeight > 100)
								temp += "TOO HEAVY";
							else if(intWeight > 36 || intLength > 36 || intHeight > 36)
								temp += "TOO BIG";
							else
								temp += "TRUCK FULL";
							
							System.out.println(temp);
							fout.println(temp);
						} // End inner catch
						
				
						
					} // End if statement
	
				
				else if((trackingNumber % 10) == 0) // Its a letter (maximum weight 24 ounces and each side is 18 inches)
					{
						stringWeight = fin.nextLine();
						stringLength = fin.nextLine();
						stringWidth = fin.nextLine();
						
					try
						{	intWeight = Integer.parseInt(stringWeight);
							intLength = Integer.parseInt(stringLength);
							intWidth = Integer.parseInt(stringWidth);
							_packageArray.add(new Letter(trackingNumber, intWeight, intLength, intWidth));
							System.out.println(_packageArray.get(x));
							fout.println(_packageArray.get(x));
							x++;
						
							if(intWeight > 24 || intWeight > 18 || intLength > 18 || numberOfPackages > _numberOfPackages)
								throw new PackageException("NOT LOADED: ");
					
							_packageArray2.add(_packageArray.get(x - 1));
							System.out.println("LOADED");
							fout.println("LOADED");
							numberOfPackages ++;
							_totalOunces += intWeight;
					
						} //End inner try
					catch(PackageException e)
						{
							String temp = e.getMessage();
							if (intWeight > 24)
								temp += "TOO HEAVY";
							else if(intWidth > 18 || intLength > 18)
								temp += "TOO BIG";
							else
								temp += "TRUCK FULL";
							
							System.out.println(temp);
							fout.println(temp);
						} // End inner catch
						
					} // End else if statement
	
				else
					{
						stringWeight = fin.nextLine();
						stringLength = fin.nextLine();
						stringWidth = fin.nextLine();
						stringHeight = fin.nextLine();
						try
						{
						
							intWeight = Integer.parseInt(stringWeight);
							intLength = Integer.parseInt(stringLength);
							intWidth = Integer.parseInt(stringWidth);
							intHeight = Integer.parseInt(stringHeight);
						
						
							throw new PackageException("NOT LOADED: ");
					
						} // End inner try
						catch(PackageException e)
						{
						
							String temp = "\n\nPackage Type: " + "unknown package type\n";
							temp += "Tracking Number: " + trackingNumber;
							temp += "\nWeight: " + intWeight;
							temp += "\nLength: " + intLength;
							temp += "\nWidth: " + intWidth;
							temp += "\nHeight: " + intHeight + "\n";
							temp += e.getMessage() + "UNKNOWN PACKAGE";
				
							System.out.println(temp);
							fout.println(temp);
				
						} // End inner catch
						
					} // End else clause 
			} // End try
			
			catch(NumberFormatException e)
			{
				String temp = "\n\nPackage Type: ";
				if((trackingNumber % 10) == 0)
					temp += "letter";
				else if((trackingNumber % 10) == 1)
					temp += "box";
				else
					temp += "unknown Package";
				temp += "\nTracking Number: " + trackingNumber;
				temp += "\nNOT LOADED: INVALID INFORMATION";
				
				System.out.println(temp);
				fout.println(temp);
				
			} // End catch			
			
		} // End while loop that reads package info
		
	} // End method loadTruckWithPackages
	
	public void driveTruck(PrintWriter fout)
	{
        /*ans bl*/
		fout.println("\n\nPACKAGE UNLOADING INFORMATION" + "\n-----------------------------");
		System.out.println("\n\nPACKAGE UNLOADING INFORMATION" + "\n-----------------------------");
        /*ans ab*/
	} // End method driveTruck
	
	
	public void unloadPackages(PrintWriter fout)
	{
			Package [] packageArray3 = new Package[_packageArray2.size()];
			for(int x = 0; x < _packageArray2.size(); x++)
				packageArray3[x] = _packageArray2.get(x);

			Arrays.sort(packageArray3);
		
			for(int x = 0; x < packageArray3.length; x++)
				{
					fout.println(packageArray3[x]);
					System.out.println(packageArray3[x]);
				} // End for loop
				
	} // End method driveTruck
	
	public void finalTruckInformation(PrintWriter fout)
	{
		String temp = "\n\nFINAL TRUCK INFORMATION";
		temp += "\n-----------------------\n";
		temp += "Packages Delivered: " + _packageArray2.size();
		temp += "\nTotal weight of packages: " + _totalPounds + " pounds " + "and " + _totalOunces + " ounces ";
		double totalWeight = _totalPounds + ((double)_totalOunces / 16);
		temp += "OR " + totalWeight + " pounds";
		
		fout.println(temp);
		System.out.println(temp);
	} // End method finalTruckInformation()
} // End class Truck